//
//  ForgotPasswordViewController.m
//  iWeddingDirectory
//
//  Created by Tung Ngo on 6/6/12.
//  Copyright (c) 2012 Home. All rights reserved.
//

#import "ForgotPasswordViewController.h"
#import "API.h"

@implementation ForgotPasswordViewController

@synthesize textFieldEmail;
@synthesize waitingIndicator;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"Forgot Password";
}

- (void)viewDidUnload
{
    [self setTextFieldEmail:nil];
    [self setWaitingIndicator:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {
    [textFieldEmail release];
    [waitingIndicator release];
    [super dealloc];
}

#pragma mark - Notification Handlers

- (void)onForgotPassword:(NSNotification *)notification
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kForgotPasswordNotification object:nil];
    
    [waitingIndicator stopAnimating];
    [self.view setUserInteractionEnabled:YES];
    
    NSString *status = [notification.userInfo objectForKey:@"status"];
    if (status == nil || [status isEqualToString:@"error"]) {
        NSString *message = [notification.userInfo objectForKey:@"error"];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
        return;
    }
    else if ([status isEqualToString:@"ok"]) {
        NSString *message = [notification.userInfo objectForKey:@"success"];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
        return;
    }
}

#pragma mark - Button Clicked Handlers

- (IBAction)onButtonSubmitClicked:(id)sender {    
    NSString *email = textFieldEmail.text;
    if (email == nil || email.length == 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Email is required" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
        return;
    }
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onForgotPassword:) name:kForgotPasswordNotification object:nil];
    
    [waitingIndicator setHidden:NO];
    [waitingIndicator setHidesWhenStopped:YES];
    [waitingIndicator startAnimating];
    [self.view setUserInteractionEnabled:NO];
    
    API *api = [API sharedInstance];    
    [api forgotPasswordWithEmail:email];
}
@end
